package com.lee.webapp.action;

import com.lee.webapp.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller("DataController")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DataController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private DataService service;

    @RequestMapping("/put")
    public void post(@RequestParam Map<String, Object> param, HttpServletResponse res) throws IOException {
        log.info(param.toString());
        service.writeData(param.get("data").toString());
        res.setContentType("text/json;charset=UTF-8");
        res.getOutputStream().write("{success:true}".getBytes());
        res.flushBuffer();
    }

    @RequestMapping("/get")
    public void get(HttpServletResponse res) throws IOException {
        String data = service.getData();
        res.setContentType("text/json;charset=UTF-8");
        res.getOutputStream().write(data.getBytes("UTF-8"));
        res.flushBuffer();
    }

}
